package DTO;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import database.DatabaseHelper;

public class AnswerDTO {
	
	private String ID;
	private String QuestionID;
	private String Content;
	
	//Property ID
	public String getID() { return ID; }
	public void setID(String id) { ID = id; }
	
	//Property QuestionID
	public String getQuestionID() { return QuestionID; }
	public void setQuestionID(String questionID) { QuestionID = questionID; }
	
	//Property Content
	public String getContent() { return Content; }
	public void setContent(String content) { Content = content; }
	
	//Methods
	
	public String createID(int number)
	{
		String result = "AS";
		if(number<10)
		{
			result+="000"+number;
		}
		else if(number<100)
		{
			result+="00"+number;
		}else if(number<1000)
		{
			result+="0"+number;
		}else
		{
			result+=number;
		}
		return result;
	}
	
	//Get Answers by QuestionID
	public static ArrayList<AnswerDTO> getAnswers(Context context, String questionID){

		ArrayList<AnswerDTO> answers = new ArrayList<AnswerDTO>();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	Cursor cursor = null;
	 	try{
	 		cursor = myDbHelper.getMyDataBase().rawQuery("select * from Answers where QuestionID = '" + questionID + "'", null);
	 	}catch(Exception e){
	 		
	 	}
		if(cursor.moveToFirst()){
			do{
				AnswerDTO answer = new AnswerDTO();
				
				//Set value to properties
				answer.setID(cursor.getString(cursor.getColumnIndex("ID")));
				answer.setQuestionID(cursor.getString(cursor.getColumnIndex("QuestionID")));
				answer.setContent(cursor.getString(cursor.getColumnIndex("Content")));
				answers.add(answer);
			}while(cursor.moveToNext());
		}
		myDbHelper.close();
	 	return answers;
	}
	
	
}
